/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.ui.properties.tabbed;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.PartDecomposition;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.tabbedproperties.AbstractSelectionElementSection;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagram.sequence.ui.filters.LifelineDecomposedAsFilter;

/**
 * Property view section to set the decomposedAs property of a {@link Lifeline}
 * 
 */
public class LifelineDecomposedAsSection extends AbstractSelectionElementSection {
	
	public LifelineDecomposedAsSection() {
		super("Decomp.:", new LifelineDecomposedAsFilter());
	}

	@Override
	public NamedElement getReferencedElementToDisplay() {
		return ((Lifeline) umlElement).getDecomposedAs();
	}

	@Override
	public void removeReferencedElement() {
		((Lifeline) getElement()).setDecomposedAs(null);	
	}

	@Override
	public void setReferencedElement(NamedElement element) {
		if (element instanceof Interaction) {
			Interaction interaction = (Interaction) element; 
			if (interaction != null) {
				PartDecomposition partDecomposition =
					UMLFactory.eINSTANCE.createPartDecomposition();
				partDecomposition.setRefersTo(interaction);
				
				String partName = 
					ModelUtil.createUniqueName(
							((Lifeline)umlElement).getNamespace(), 
							"ref_"+interaction.getName());
				partDecomposition.setName(partName);
				
				((Lifeline) getElement()).setDecomposedAs(partDecomposition);
				((Lifeline) getElement()).getInteraction().getFragments().add(partDecomposition);
			}
			
		} else if (element instanceof PartDecomposition) {
			((Lifeline) getElement()).setDecomposedAs((PartDecomposition) element);
			
		}
	}

	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		
		super.createControls(parent, tabbedPropertySheetPage);
		label.setToolTipText("Decomposed as");
	}
}

